const puppeteer = require('puppeteer')
const fs = require('fs')

module.exports =  async () => {
  let data = {}
  const browser = await puppeteer.launch({
    headless: true,
    userDataDir: './data',
  })
  const page = await browser.newPage()
  for (let mon = 7; mon < 8; mon++) {
    const monData = (data[`mon_${mon}`] = {})
    for (let pg = 1; pg <= 10; pg++) {
      mon = mon.toString().padStart(2, '0')
      await page.goto(
        `https://www.bilibili.com/v/tech/computer_tech/#/all/click/0/${pg}/2021-${mon}-1,2021-${mon}-10`
      )
      const selector =
        '#videolist_box > div.vd-list-cnt > ul > li > div > div.r > a'
      await page.waitForSelector(selector)
      let titles = await page.$$eval(selector, (links) =>
        links.map((x) => x.textContent)
      )
      console.log(titles)
      monData[pg] = titles
    }
  }
  await browser.close()
  // fs.writeFileSync('data.json', JSON.stringify(data, null, '\t'))
  return data
}
